package org.yray.sip.creator;


import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;

import java.util.Optional;


public class AckCreator extends MessageCreator {
    /***
     * ACK sip:78002@172.168.200.78:5060;transport=udp SIP/2.0
     Via: SIP/2.0/UDP 172.130.13.31:59294;rport;branch=z9hG4bKPj51b27d77bdc54839b4c2f2c8d8e82953
     Max-Forwards: 70
     From: "78004" <sip:78004@172.168.200.78>;tag=07ff3ad89b044e0c87ecd66327ddfd28
     To: <sip:78002@172.168.200.78>;tag=1HKjBD92tycyB
     Call-ID: a344286ebd0340b393df84abc9494f3a
     CSeq: 20063 ACK
     Content-Length:  0
     * @param session
     * @return
     */
    @Override
    public String createMsg(SipSession session){

        StringBuilder sb = new StringBuilder();
        sb.append("ACK sip:"+session.getTo()+"@"+session.getSipIpPort()+";transport=udp SIP/2.0").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP "+ session.getLocalIpPort()+";rport;branch="+session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Max-Forwards: 70").append(MsgEle.LINE_SEPARATOR);
        Optional.ofNullable(session.getSipMap()).ifPresent(
                map->{
                    Optional.ofNullable(session.getSipMap().get(MsgEle.CALL_ID)).ifPresent(v->{
                        sb.append(MsgEle.CALL_ID+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.FROM)).ifPresent(v->{
                        sb.append(MsgEle.FROM+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.TO)).ifPresent(v->{
                        sb.append(MsgEle.TO+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                }
        );
        sb.append("CSeq: "+session.getCseq()+" ACK").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:  0").append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }


}
